<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/15
 * Time: 23:05
 */
namespace app\admin\logic\statistics;




use app\common\model\zbtask\Rate;
use app\common\utils\Utils;
use Monolog\Handler\IFTTTHandler;
use think\exception\ValidateException;

class RateStatistics
{
    private static $new; //申请一个私有的静态成员变量来保存该类的唯一实例
    private $where = [];
    public $model;
    private $monthTime = [];
    private $currentMonth = [];
    public $sumInfo = [];
    private function __construct() {
        $this->request = request();
        $this->model = new Rate();
    }


    public static function instance () { //声明一个静态公共方法，供外部获取唯一实例
        if (!(self::$new instanceof self)) {
            self::$new = new self;
        }
        return self::$new;
    }
    /**
     * 获取某一个月的数据 月份开始时间 和月份结束时间
     * @param $timeStr
     * @return $this
     */
    public function setYearToMonthTime($timeStr) {
        $timeArr = Utils::explodeStr($timeStr, '-');
        if (count($timeArr) !== 2) {
            throw new ValidateException('设置月份失败');
        }
        $timeSt = Utils::getStartAndEndUnixTimestamp($timeArr[0], $timeArr[1]);  //当前月开始时间和结束时间
        $year = Utils::getStartAndEndUnixTimestamp($timeArr[0]);  //获取选取年开始时间

        $this->currentMonth = [$timeSt['start'], $timeSt['end']];
        $this->monthTime = [$year['start'], $timeSt['end']];

        return $this;
    }
    /**
     * 获取本次查询的数据
     * @param array $where
     * @return object;
     */
    public function getList($where = []) {
        $field = ['rate.ca_tax','rate.tax_rate','rate.id', 'rate.user_id', 'sum(rate.money) as money','user.real_name', 'user.identity_card', 'user.mobile'];
        $map = [];
        $map['rate.status'] = 0; //有效的众包费
        if ($this->currentMonth) {
            $map['rate.createtime'] = ['between', $this->currentMonth];
        }
        //根据用户ID和税率分组
        $list = $this->model->with(['user', 'company'])
            ->where($where)->where($map)
            ->group('rate.user_id,rate.tax_rate')->field($field)->select();

        return $list;
    }

    /**
     * 生成行数据
     * @param int $sort 排序
     * @param array $value 行值
     * @return array
     */
    public function getExportValues($sort, $value) {
        $map['user_id'] = $value['user_id'];
        if (!$this->monthTime) {
            $map['createtime'] = ['between', $this->monthTime]; //本期最大时间
        }

        $statistics = $this->model
            ->where($map)
            ->field(['round(sum(ca_money/ca_tax), 2) as sum_zhongbaofei', 'sum(money) as sum_money', 'count(*) as countnum'])->find();
        //sheet1统计信息  累计本月信息
        $month_statistics = $this->model
            ->where(['user_id' => $value['user_id'], 'createtime' =>['between', $this->currentMonth]])
            ->field(['round(sum(ca_money/ca_tax), 2) as sum_zhongbaofei', 'sum(money) as sum_money', 'count(*) as countnum'])->find();
        if ($month_statistics['countnum'] > 1) {  //本月
            $remarkValues = [$value['user']['identity_card']."\t", $value['user']['real_name'], $month_statistics['countnum'],$month_statistics['sum_zhongbaofei'],
                $statistics['countnum'],$statistics['sum_zhongbaofei']];
            array_push($this->sumInfo, $remarkValues);
        }
        //总统计信息
        return [
            $sort,//序号
            $value['user']['real_name'],  //真实姓名
            '居民身份证',  //证件类型3
            $value['user']['identity_card']."\t",  //身份证件
            '中国',
            $value['user']['mobile'],
            '天津市',
            $statistics['sum_zhongbaofei'], //加个条件限制今年
            $value['ca_tax'],       //应税所得率
            '', //计税依据,
            $value['tax_rate'], //税率
            $value['kouchushu'] , //总速算扣除数
            $statistics['sum_money'] , //总税额
            Utils::processMoney($statistics['sum_money'], $value['money'], '-', 2) , //总税额
            $value['money']   //本期 只本月
        ];
    }


}